/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import beans.SeekerCustomer;
import java.io.*;
import java.util.HashMap;
import javax.servlet.*;
import javax.servlet.http.*;
/**
 *
 * @author Dhruv
 */

//servlet class acts as controller by delegating
//operations to the respective Action concrete subclass
public class ControllerServlet extends HttpServlet {

    //stores all the possible operation names
    //and operation objects for quick access
    private HashMap actions;

    @Override
    public void init() throws ServletException {
        actions = new HashMap();

        //all the various operations are stored in the hashmap
        CreateUserAction cua = new CreateUserAction(new SeekerCustomer());
        actions.put(cua.getName(), cua);
        ValidateUserAction vua = new ValidateUserAction(new SeekerCustomer());
        actions.put(vua.getName(), vua);
        ListNonFriendsAction lnfa = new ListNonFriendsAction(new SeekerCustomer());
        actions.put(lnfa.getName(), lnfa);
        AddFriendAction afa = new AddFriendAction(new SeekerCustomer());
        actions.put(afa.getName(), afa);
        ConfirmFriendReqAction cfra = new ConfirmFriendReqAction(new SeekerCustomer());
        actions.put(cfra.getName(),cfra);
        DeclineFriendReqAction dfra = new DeclineFriendReqAction(new SeekerCustomer());
        actions.put(dfra.getName(),dfra);
        AddImageAction aia = new AddImageAction(new SeekerCustomer());
        actions.put(aia.getName(),aia);
        ViewImageAction via = new ViewImageAction(new SeekerCustomer());
        actions.put(via.getName(),via);
        ViewAllImagesAction vaia = new ViewAllImagesAction(new SeekerCustomer());
        actions.put(vaia.getName(),vaia);
        AddTagAction ata = new AddTagAction(new SeekerCustomer());
        actions.put(ata.getName(),ata);
        ViewTagAction vta = new ViewTagAction(new SeekerCustomer());
        actions.put(vta.getName(),vta);
        ViewAllTagsAction vata = new ViewAllTagsAction(new SeekerCustomer());
        actions.put(vata.getName(),vata);
        ViewProfileAction vpa = new ViewProfileAction(new SeekerCustomer());
        actions.put(vpa.getName(),vpa);
        EditAccountAction epa = new EditAccountAction(new SeekerCustomer());
        actions.put(epa.getName(),epa);
        ViewOthersImageAction voia = new ViewOthersImageAction(new SeekerCustomer());
        actions.put(voia.getName(), voia);
        AddOthersTagAction aota = new AddOthersTagAction(new SeekerCustomer());
        actions.put(aota.getName(),aota);
        LogoutAction loa = new LogoutAction(new SeekerCustomer());
        actions.put(loa.getName(), loa);
        ToptagsAction tts = new ToptagsAction(new SeekerCustomer());
        actions.put(tts.getName(), tts);
        UpdateAccountAction uaa = new UpdateAccountAction(new SeekerCustomer());
        actions.put(uaa.getName(), uaa);
        ViewAllFriendsAction vafa = new ViewAllFriendsAction(new SeekerCustomer());
        actions.put(vafa.getName(), vafa);
        ReturnHomeAction rha = new ReturnHomeAction(new SeekerCustomer());
        actions.put(rha.getName(),rha);
    }
    
    public void processRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        //identify the operation from the URL
        String op = getOperation(req.getRequestURL());
        //find and execute corresponding Action
        Action action = (Action)actions.get(op);
        Object result = null;
        try {
            //maintain the session between requests
            result = action.perform(req, resp);
            HttpSession session = req.getSession();
            session.setAttribute("session1", result);
        } catch (NullPointerException npx) {
            //nothing to handle
        }
    }

    //both GET and POST operations are directed to "processRequest" method
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    //uses the URL to identify the operation
    private String getOperation(StringBuffer requestURL) {

        String op="";
        //identifies the last index of "/" before ".do" and 
        //uses that to put each character after the "/" into "op"
        for(int i= requestURL.lastIndexOf("/",requestURL.indexOf(".do"))+1; i<requestURL.indexOf(".do"); i++)
        {
            op= op+requestURL.charAt(i);
        }
        return op;
    }
}
